package com.ann.concurrent;

public class VolatileAtomicTest {
    public static volatile int num = 0;
    public static void increase(){
        num++;
    }

    public static void main(String[] args) throws InterruptedException {
        Thread[] threads = new Thread[10];
        for (int i = 0; i < threads.length; i++) {
            threads[i] = new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int j = 0; j < 1000; j++) {
                        increase();
                    }
                }
            });
            threads[i].start();
        }

        for (Thread t : threads) {
            t.join();//通过join方法来让主线程等待线程执行的结果，其实有点类似future/callable的功能。
        }

        System.out.println(num);// 1000*10 = 10000
        /**
         * 实际执行结果  8489  9910  9880  10000  9240
         *
         * 小于等于10000
         * 因为在num  刷回主内存时，其他线程依旧执行，导致某些increase执行失效
         */
    }
}
